<?php

class QuestionController extends BaseController
{
    protected $tableName = "question";
    protected $pk = "question_id";

    // 新增问题接口
    public function post($obj)
    {
        $user_id = $obj['user_id'];
        $question_title = $obj['question_title'];
        $question_detail = $obj['question_detail'];
        $sql = "INSERT INTO question VALUES (0, $user_id, '$question_title', '$question_detail', CURRENT_TIME, CURRENT_TIME)";
        $result = $this->conn->query($sql);
        if ($this->judge($result)) {
            $newRecord = $this->getNewRecord($obj);
            return ReturnHelper::success($newRecord,"用户`$user_id`新建问题成功");
        }
        else {
            return ReturnHelper::failed([],"用户`$user_id`新建问题失败：" . $this->conn->error);
        }
    }

    public function get($id)
    {
        $sql = "SELECT u.nickname, u.avatar_url, q.* 
                FROM question q, user u
                WHERE q.user_id = u.user_id AND question_id = $id";
        $result = $this->conn->query($sql);
        $data = $this->getSingleDataFromResult($result);
        return ReturnHelper::success($data, "问题`$id`请求成功");
    }

    public function getUserQuestion($id)
    {
        $sql = "select q.*, u.avatar_url, u.nickname, ifnull(t1.ans_num, 0) as ans_num
                from question q left join user u on u.user_id = q.user_id
                    left join (select a.question_id, count(a.question_id) as ans_num
                    from answer a group by a.question_id) t1 on t1.question_id = q.question_id
                where q.user_id = $id
                group by q.question_id";
        $result = $this->conn->query($sql);
        $data = $this->getArrayDataFromResult($result);
        return ReturnHelper::success($data, "用户`$id`提问请求成功");
    }

    // GET 获取所有问题和该问题下的高赞回答
    public function getList()
    {
        // 添加是否点赞的查询
        $sqlPart1 = "FALSE 'is_thumb_up'";
        $sqlPart2 = "FALSE 'is_collect'";
        $byLoginUser = "FALSE 'byLoginUser'";
        $login = LoginController::getLoginInfo();
        if ($login['isLogin']) {
            $user_id = $login['user_id'];
            $sqlPart1 = "IF((
                    SELECT id
                    FROM answer_thumb_up atu
                    WHERE atu.user_id = $user_id AND answer_id = a.answer_id
                ) IS NULL, FALSE, TRUE) 'is_thumb_up'";
            $sqlPart2 = "IF((
                    SELECT collect_id
                    FROM user_collect uc
                    WHERE uc.user_id = $user_id AND uc.answer_id = a.answer_id
                ) IS NULL, FALSE, TRUE) 'is_collect'";
            // 判断这个回答是否是当前登录用户的回答
            $byLoginUser = "IF(a.user_id = $user_id, true, false) 'byLoginUser'";
        }
        $sql = "SELECT '问题部分', joinTable.*, question_user.nickname 'question_user_nickname', question_user.avatar_url 'question_user_avatar',
                '高赞回答部分', a.answer_id, a.user_id 'answer_user_id', answer_user.nickname 'answer_user_nickname', answer_user.avatar_url 'answer_user_avatar', 
                answer_content, answer_text, thumb_up, comment_num, collect_num, a.create_time 'answer_create_time', modify_time 'answer_modify_time',
                $sqlPart1, $sqlPart2, $byLoginUser
                FROM(
                    SELECT user_id, question_id, question_title, question_detail, create_time 'question_create_time', modify_time 'question_modify_time',(
                        SELECT a.answer_id 
                        FROM answer a
                        WHERE a.question_id = q.question_id
                        ORDER BY a.thumb_up DESC, a.collect_num DESC, a.create_time DESC
                        LIMIT 1
                    ) main_answer_id
                    FROM question q
                )joinTable
                LEFT JOIN answer a ON joinTable.main_answer_id=a.answer_id
                LEFT JOIN user answer_user ON a.user_id = answer_user.user_id
                LEFT JOIN user question_user ON joinTable.user_id = question_user.user_id";

//        echo $sql; die;
        $result = $this->conn->query($sql);
        $data = $this->getArrayDataFromResult($result);
        return ReturnHelper::success($data, "数据请求成功");
    }


    // GET
    public function getAnswers($question_id) {
        $question = $this->get($question_id)['data'];
        if (empty($question)) {
            return ReturnHelper::failed([],"问题id有误");
        }
        // 添加是否点赞的查询
        $sqlPart1 = "FALSE 'is_thumb_up'";
        $sqlPart2 = "FALSE 'is_collect'";
        $byLoginUser = "FALSE 'byLoginUser'";
        $login = LoginController::getLoginInfo();
        if ($login['isLogin']) {
            $user_id = $login['user_id'];
            $sqlPart1 = "IF((
                    SELECT id
                    FROM answer_thumb_up atu
                    WHERE atu.user_id = $user_id AND answer_id = a.answer_id
                ) IS NULL, FALSE, TRUE) 'is_thumb_up'";
            $sqlPart2 = "IF((
                    SELECT collect_id
                    FROM user_collect uc
                    WHERE uc.user_id = $user_id AND uc.answer_id = a.answer_id
                ) IS NULL, FALSE, TRUE) 'is_collect'";
            // 判断这个回答是否是当前登录用户的回答
            $byLoginUser = "IF(a.user_id = $user_id, true, false) 'byLoginUser'";
        }
        $sql = "SELECT u.nickname, u.avatar_url, u.signature, a.*, $sqlPart1, $sqlPart2, $byLoginUser
                FROM answer a, `user` u
                WHERE a.user_id = u.user_id AND question_id=$question_id
                ORDER BY a.thumb_up DESC, a.collect_num DESC, a.create_time DESC";
        $result = $this->conn->query($sql);
        $data = $this->getArrayDataFromResult($result);
        if (empty($data)) {
            return ReturnHelper::failed([],"该问题答案为空");
        }
        else {
            return ReturnHelper::success($data, "数据请求成功");
        }
    }

    public function update($id, $obj)
    {
        $user_id = $obj['user_id'];
        $getRes = $this->get($id)['data'];
        if ($getRes['user_id'] != $user_id) {
            return ReturnHelper::failed([], "你不是提问该问题的作者，无法修改这篇文章");
        }
        return parent::update($id, $obj);
    }
}